package com.example.notepad.litepal;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.notepad.R;
import com.example.notepad.utils.DBUtils;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tvNoteTime;
    private TextView tvNoteName;
    private EditText etContent;
    private NotepadDao notepadDao;

    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        initView();
        initData();
    }

    private void initData() {
        notepadDao = new NotepadDao();
        tvNoteName.setText("添加记录");

        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Notepad note = (Notepad) bundle.getSerializable("note");
                if (note != null) {
                    id = String.valueOf(note.getId());
                    tvNoteName.setText("修改记录");
                    etContent.setText(note.getContent());
                    tvNoteTime.setText(note.getTime());
                    tvNoteTime.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    private void initView() {
        tvNoteName = findViewById(R.id.tv_name);
        tvNoteTime = findViewById(R.id.tv_time);
        etContent = findViewById(R.id.et_content);

        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_delete).setOnClickListener(this);
        findViewById(R.id.iv_save).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_delete:
                etContent.setText("");
                break;
            case R.id.iv_save:
                String content = etContent.getText().toString().trim();
                Notepad notepad = new Notepad();
                if (id != null) {
                    notepad.setId(Integer.parseInt(id));
                }
                if (content.length() == 0) {
                    showToast("内容不能为空！");
                    return;
                }
                notepad.setContent(content);
                notepad.setTime(DBUtils.getTime());
                if (id != null) {
                    if (notepadDao.update(notepad)) {
                        showToast("修改成功");
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        showToast("修改失败");
                    }
                } else {
                    if (notepadDao.insert(notepad)) {
                        showToast("保存成功");
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        showToast("保存失败");
                    }
                }
                break;
        }
    }

    private void showToast(String msg) {
        Toast.makeText(RecordActivity.this, msg, Toast.LENGTH_LONG).show();
    }
}